﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_menuManager : BaseListPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        PageSize = 10;
        TableName = "Menu"; Pk = "MenuId";
        if (!this.IsPostBack)
        {
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
            BindDDL();
        }
    }
    private void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref menulist,
            MenuBLL.GetInstance().GetList(r=>r.ParentMenu==0), "MenuName", "MenuId", "一级菜单");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("menuAdd.aspx");
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        string[] id = SelectId();
        if (id != null)
        {
            foreach (var i in id)
            {
                int menuid = Convert.ToInt32(i);
                var t = MenuBLL.GetInstance().GetEntity(r => r.MenuId == menuid);
                if (t.Status == 0)
                {
                    t.Status = 1;
                }
                else
                {
                    t.Status = 0;
                }
                MenuBLL.GetInstance().Update(t);
            }
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
    }

    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Condition = "";
        string menuid = this.menulist.SelectedValue;

        if (!menuid.Equals("-1"))
        {
            Condition = Condition + " and ParentMenu = " + menuid;
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }
    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        PageIndex = Pager.CurrentPageIndex - 1;
        BindData(ref rptList);
    }

    public string getParentName(object menuid)
    {
        string result = "未知";
        try {
        int id = Convert.ToInt32(menuid);
        if(id==0)
        {
            result = "一级菜单";
        }
        else
        {
            result = MenuBLL.GetInstance().GetEntity(r => r.MenuId == id).MenuName;
        }
        }
        catch { }
        return result;
    }
    public string getStatus(object statusid)
    {
        string result = "未知";
        try
        {
            int status = Convert.ToInt32(statusid);
            if (status == 1)
            {
                result = "启用中";
            }
            else
            {
                result = "<font color='red'>已禁用</font>";
            }
        }
        catch { }
        return result;
    }
    private string[] SelectId()
    {
        string[] id = null;
        string ids = "";
        try
        {
            ids = Request["ids"];
            if (ids.Length == 0)
            {
                UtilityMessageBox.Show(this, "未选择");
                return null;
            }
        }
        catch
        {
            UtilityMessageBox.Show(this, "未选择");
            return null;
        }
        id = ids.Split(',');
        return id;
    }
}